package com.yl.chainStore.services.impl.statistical


import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.entity.pay.PayOrderCustomerPayLog
import com.yl.chainStore.entity.pay.PayOrderCustomerPayLogDetail
import com.yl.chainStore.entity.pay.PayOrderRefundLog
import com.yl.chainStore.mapper.pay.*
import com.yl.chainStore.pojo.dto.statistical.*
import com.yl.chainStore.pojo.vo.pay.PayCustomerOrderPayAndRefundLogSelectVo
import com.yl.chainStore.pojo.vo.pay.PayOrderCustomerPayLogSelectVo
import com.yl.chainStore.pojo.vo.statistical.DailyCheckForSelectVo
import com.yl.chainStore.pojo.vo.statistical.IncomeSummarySelectVo
import com.yl.chainStore.pojo.vo.statistical.ReconciliationSelectVo
import com.yl.chainStore.services.statistical.IPayStatisticalService
import com.yl.chainStore.util.dayFormat
import com.yl.common.getContext
import org.springframework.stereotype.Service
import java.text.SimpleDateFormat


@Service
class PayStatisticalServiceImpl(
	private val payCustomerOrderMapper: PayCustomerOrderMapper,

	private val payCustomerBalanceInLogMapper: PayCustomerBalanceInLogMapper,

	private val payOrderCustomerPayLogDetailMapper: PayOrderCustomerPayLogDetailMapper,

	private val payOrderCustomerPayLogMapper: PayOrderCustomerPayLogMapper,

	private val payCustomerBalanceToMoneyLogMapper: PayCustomerBalanceToMoneyLogMapper,

	private val payOrderRefundLogMapper:PayOrderRefundLogMapper,

	private val payCustomerGiftsOfGoldMapper: PayCustomerGiftsOfGoldMapper,


	):IPayStatisticalService {


	override fun getOrderStatisticalByDay(vo: IncomeSummarySelectVo): List<StatisticalOrderCollectionDto> {
		return payCustomerOrderMapper.getOrderStatisticalByDay(vo)
	}

	override fun getCustomerBalanceInLogByDay(vo: IncomeSummarySelectVo): List<StatisticalBalanceInLogByDayDto> {
		return payCustomerBalanceInLogMapper.getCustomerBalanceInLogByDay(vo)
	}

	override fun getCustomerOrderPayLogDetailByDay(vo: IncomeSummarySelectVo): List<StatisticalBalanceInLogByDayDto> {
		return payOrderCustomerPayLogDetailMapper.getCustomerOrderPayLogDetailByDay(vo)
	}

	override fun getCustomerOrderUseGiftMoneyByDay(vo: IncomeSummarySelectVo): Map<String, Long> {
		val map = mutableMapOf<String,Long>()
		val useGiftMoney = payCustomerGiftsOfGoldMapper.getCustomerOrderUseGiftMoneyByDay(vo)
		for(gift in useGiftMoney){
			map[gift.payDay] = gift.inSumMoney
		}
		return map
	}

	override fun getCustomerOrderRefundByDay(vo: IncomeSummarySelectVo): Map<String, Long> {
		val map = mutableMapOf<String,Long>()
		val refundMoneys = payOrderRefundLogMapper.getCustomerOrderRefundByDay(vo)
		for(refundMoney in refundMoneys){
			map[refundMoney.payDay] = refundMoney.inSumMoney
		}
		return map
	}

	override fun getCustomerOrderPayLogSumMoneyByDay(vo: IncomeSummarySelectVo): List<OrderPayLogSumMoneyDto> {
		return payOrderCustomerPayLogDetailMapper.getCustomerOrderPayLogSumMoneyByDay(vo)
	}

	override fun getOrderPayLog(vo: DailyCheckForSelectVo): List<PayOrderCustomerPayLog> {
		val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
		val payLogList = payOrderCustomerPayLogMapper.getPayLogList(
			PayOrderCustomerPayLogSelectVo(
				payDay = simpleDateFormat.parse(vo.dayString),
				subCompanyId = getContext().subCompanyId,
				companyId = getContext().companyId
			)
		)

		for(payLog in payLogList){
			payLog.details = payOrderCustomerPayLogDetailMapper.selectList(Wrappers.query<PayOrderCustomerPayLogDetail?>()
				.eq("pay_log_id",payLog.id).orderByAsc("id"))
		}
		return payLogList
	}

	override fun getPayCustomerOrderPayAndRefundLog(vo: DailyCheckForSelectVo): List<PayCustomerOrderPayAndRefundLogDto> {
		return payOrderCustomerPayLogMapper.getPayCustomerOrderPayAndRefundLog(
			PayCustomerOrderPayAndRefundLogSelectVo(
				day = vo.dayString,
				subCompanyId = getContext().subCompanyId,
				companyId = getContext().companyId
			)
		)
	}

	override fun getPayCustomerOrderPayAndRefundLogExport(vo: DailyCheckForSelectVo): List<PayCustomerOrderPayAndRefundLogDto> {
		return payOrderCustomerPayLogMapper.getPayCustomerOrderPayAndRefundLogExport(
				PayCustomerOrderPayAndRefundLogSelectVo(
						startTime = vo.startTime,
						endTime = vo.endTime,
						subCompanyId = getContext().subCompanyId,
						companyId = getContext().companyId,
						orderStr = "t.payTime DESC"
				)
		)
	}

	override fun getPayCustomerOrderPayAndRefundLogPage(vo: DailyCheckForSelectVo): Page<PayCustomerOrderPayAndRefundLogDto> {
		val page = Page<PayCustomerOrderPayAndRefundLogDto>(vo.current,vo.size)
		return payOrderCustomerPayLogMapper.getPayCustomerOrderPayAndRefundLogPage(
			page,
			PayCustomerOrderPayAndRefundLogSelectVo(
				startTime = vo.startTime,
				endTime = vo.endTime,
				subCompanyId = getContext().subCompanyId,
				companyId = getContext().companyId,
				orderStr = "t.payTime DESC"
			)
		)
	}

	override fun getCustomerBalanceInLogByPayType(vo: IncomeSummarySelectVo): List<StatisticalBalanceInLogByTypeDto> {
		return payCustomerBalanceInLogMapper.getCustomerBalanceInLogByPayType(vo)
	}

	override fun getCustomerOrderPayLogSumMoneyByPayType(vo: IncomeSummarySelectVo): List<StatisticalBalanceInLogByTypeDto> {
		return payOrderCustomerPayLogDetailMapper.getCustomerOrderPayLogSumMoneyByPayType(vo)
	}

	override fun getBalanceOutLogGroupByPayType(vo: IncomeSummarySelectVo): List<StatisticalBalanceInLogByTypeDto> {
		return payCustomerBalanceToMoneyLogMapper.selectLogGroupByPayType(vo)
	}

	override fun getNewAddOwnSumMoney(vo: ReconciliationSelectVo): Long {
		vo.logPayType = TypeEnum.PAY_ORDER_PAY_LOG_PAY_TYPE_NORMAL
		return payOrderCustomerPayLogDetailMapper.getNewAddOwnSumMoney(vo)
	}

	override fun getPutAwayOwnSumMoney(vo: ReconciliationSelectVo): Long {
		vo.logPayType = TypeEnum.PAY_ORDER_PAY_LOG_PAY_TYPE_OWE
		return payOrderCustomerPayLogDetailMapper.getPutAwaySumMoney(vo)
	}

	override fun getTopUpSumMoney(vo: ReconciliationSelectVo): Long {
		vo.topInUseType = null
		return payCustomerBalanceInLogMapper.getTopUpSumMoney(vo)
	}

	override fun getPayOrderSumMoney(vo: ReconciliationSelectVo): Long {
		vo.logPayType = null
		return payOrderCustomerPayLogDetailMapper.getPutAwaySumMoney(vo)
	}

	override fun getOnlyTopUpBalanceSumMoney(vo: ReconciliationSelectVo): Long {
		vo.topInUseType = TypeEnum.PAY_TYPE_TOP_UP
		return payCustomerBalanceInLogMapper.getTopUpSumMoney(vo)
	}

	override fun getOrderPaySumMoneyByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.logPayType = null
		return payOrderCustomerPayLogDetailMapper.getOrderPaySumMoneyByDay(vo)
	}

	override fun getPutAwayOwnSumMoneyByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.logPayType = TypeEnum.PAY_ORDER_PAY_LOG_PAY_TYPE_OWE
		return payOrderCustomerPayLogDetailMapper.getOrderPaySumMoneyByDay(vo)
	}

	override fun getNewAddOwnSumMoneyByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.logPayType = TypeEnum.PAY_ORDER_PAY_LOG_PAY_TYPE_NORMAL
		return payOrderCustomerPayLogDetailMapper.getNewAddOwnSumMoneyByDay(vo)
	}

	override fun getOnlyTopUpBalanceSumMoneyByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.topInUseType = TypeEnum.PAY_TYPE_TOP_UP
		return payCustomerBalanceInLogMapper.getOnlyTopUpBalanceSumMoneyByDay(vo)
	}

	override fun getTopUpForPayOrderSumMoneyByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.topInUseType = TypeEnum.PAY_TYPE_BALANCE_ORDER_PAY
		return payCustomerBalanceInLogMapper.getOnlyTopUpBalanceSumMoneyByDay(vo)
	}

	override fun getOnlyBalanceToMoneySumByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.refundUseType = TypeEnum.PAY_TYPE_BALANCE_REFUND
		return payCustomerBalanceToMoneyLogMapper.refundSumMoneyByDay(vo)
	}

	override fun getOrderBalanceToMoneySumByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.refundUseType = TypeEnum.PAY_TYPE_ORDER_REFUND
		return payCustomerBalanceToMoneyLogMapper.refundSumMoneyByDay(vo)
	}

	override fun getOrderRefundToBalanceSumMoneyByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.orderRefundType = TypeEnum.ORDER_REFUND_TYPE_TO_BALANCE
		return payOrderRefundLogMapper.getOrderRefundSumMoneyByDay(vo)
	}

	override fun getOrderRefundToMoneySumMoneyByDay(vo: ReconciliationSelectVo): List<PaySumMoneyForDayDto> {
		vo.orderRefundType = TypeEnum.ORDER_REFUND_TYPE_TO_MONEY
		return payOrderRefundLogMapper.getOrderRefundSumMoneyByDay(vo)
	}
}
